package org.jeecg.modules.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuWalletaMnagement;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuWalletaMnagementService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 钱包管理
 * @Author: jeecg-boot
 * @Date: 2024-08-05
 * @Version: V1.0
 */
@Tag(name = "钱包管理")
@RestController
@RequestMapping("/cuckoo/cuWalletaMnagement")
@Slf4j
public class CuWalletaMnagementController extends JeecgController<CuWalletaMnagement, ICuWalletaMnagementService> {
    @Autowired
    private ICuWalletaMnagementService cuWalletaMnagementService;

    @Autowired
    private ICuUserDetailsService cuUserDetailsService;

    /**
     * 分页列表查询
     *
     * @param cuWalletaMnagement
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "钱包管理-分页列表查询")
    @Operation(summary = "钱包管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CuWalletaMnagement>> queryPageList(CuWalletaMnagement cuWalletaMnagement,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<CuWalletaMnagement> queryWrapper = QueryGenerator.initQueryWrapper(cuWalletaMnagement, req.getParameterMap());
        Page<CuWalletaMnagement> page = new Page<CuWalletaMnagement>(pageNo, pageSize);
        IPage<CuWalletaMnagement> pageList = cuWalletaMnagementService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param cuWalletaMnagement
     * @return
     */
    @AutoLog(value = "钱包管理-添加")
    @Operation(summary = "钱包管理-添加")
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CuWalletaMnagement cuWalletaMnagement) {
        cuWalletaMnagementService.save(cuWalletaMnagement);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cuWalletaMnagement
     * @return
     */
    @AutoLog(value = "钱包管理-编辑")
    @Operation(summary = "钱包管理-编辑")
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CuWalletaMnagement cuWalletaMnagement) {
        cuWalletaMnagementService.updateById(cuWalletaMnagement);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "钱包管理-通过id删除")
    @Operation(summary = "钱包管理-通过id删除")
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        cuWalletaMnagementService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "钱包管理-批量删除")
    @Operation(summary = "钱包管理-批量删除")
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cuWalletaMnagementService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "钱包管理-通过id查询")
    @Operation(summary = "钱包管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CuWalletaMnagement> queryById(@RequestParam(name = "id", required = true) String id) {
        CuWalletaMnagement cuWalletaMnagement = cuWalletaMnagementService.getById(id);
        if (cuWalletaMnagement == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(cuWalletaMnagement);
    }

    /**
     * 根据用户id查询钱包信息
     */
    @Operation(summary = "钱包管理-根据用户id查询钱包信息")
    @GetMapping(value = "/getByUserId")
    public Result<CuWalletaMnagement> getByUserId(@RequestParam(name = "userId", required = true) String userId) {
        // 获取用户信息
        CuUserDetails userDetails = cuUserDetailsService.getById(userId);
        if (ObjectUtil.isEmpty(userDetails)) {
            return Result.error("未找到对应数据");
        }
        CuWalletaMnagement cuWalletaMnagement = cuWalletaMnagementService.queryByUserId(userDetails);
        if (cuWalletaMnagement == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(cuWalletaMnagement);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cuWalletaMnagement
     */
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CuWalletaMnagement cuWalletaMnagement) {
        return super.exportXls(request, cuWalletaMnagement, CuWalletaMnagement.class, "钱包管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("cuckoo:cu_walleta_mnagement:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CuWalletaMnagement.class);
    }

    /**
     * 充值
     */
    @Operation(summary = "钱包管理-充值")
    @PostMapping(value = "/recharge")
    public Result<String> recharge(@RequestBody CuWalletaMnagement cuWalletaMnagement) {
        // 获取用户信息
        CuUserDetails userDetails = cuUserDetailsService.getById(cuWalletaMnagement.getUserId());
        if (ObjectUtil.isEmpty(userDetails)) {
            return Result.error("未找到对应数据");
        }
        cuWalletaMnagementService.payReward(userDetails, cuWalletaMnagement.getBalance());
        return Result.OK("充值成功");
    }

}
